exports.api = function(ops) {
	var app = ops.app,
		cluster = ops.cluster,
		url = ops.url,
		s_config = ops.s_config,
		routes = ops.routes,
		grunt = require('grunt'),
		//RateLimit = ops.RateLimit,
		wss = ops.wss;

	var crossHeader = function(res){
		res.header('Access-Control-Allow-Origin', 'http://192.168.110.16:8080');
		res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
		res.header('Access-Control-Allow-Methods', 'POST, GET');
		res.header('Content-Security-Policy', "default-src 'self' 192.168.110.16:*");
	}

	var filter = function(ip, res) {
			/*var a = grunt.file.readJSON(path.resolve("user/ip.json"));
			ip = ip.split('.');
			//console.log(new RegExp(ip.join('\\.'), "gim"), new RegExp(ip.join('\\.'), "gim").test(a.data), a.data)
			if (new RegExp(ip.join('\\.'), "gim").test(a.data)) {
				console.log("filter: " + ip)
					//routes.err(res, '404');
				return false;
			}*/
			return true;
		},
		error = function(res, req, bool) {
			var query = url.parse(req.url, true).query;
			var b = query.callback;
			res.send(bool ? {
				status: 0,
				data: []
			} : b ? b + "(" + JSON.stringify({
				status: 0,
				data: []
			}) + ")" : {
				status: 0,
				data: []
			});
		};

	app.get("/", function(req, res) {
		console.log('worker /' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;
			routes.index(s_config, res, query);
		} else {
			routes.err(res, '404');
		}
	});

	app.post(/\/getHomeFeed/, function(req, res){
		console.log('worker /channel - ' + cluster.worker.id)
		console.log("PATH: " + req.url)
		//crossHeader(res)
		routes.getHomeFeed(res, req);
	})

	app.post(/\/cityWideItems/, function(req, res){
		console.log('worker /channel - ' + cluster.worker.id)
		console.log("PATH: " + req.url)
		//crossHeader(res)
		routes.getCityWideItems(res, req);
	})

	app.post(/\/recommendItems/, function(req, res){
		console.log('worker /channel - ' + cluster.worker.id)
		console.log("PATH: " + req.url)
		//crossHeader(res)
		routes.getRecommendItems(res, req);
	})

	/*var intervalTasksLimiter = new RateLimit({
		windowMs: 3 * 1000, // 1 hour window
		delayAfter: 1, // begin slowing down responses after the first request
		delayMs: 1 * 500, // slow down subsequent responses by 3 seconds per request
		max: 5, // start blocking after 5 requests
		message: "Too many accounts created from this IP, Please try again after 30 minutes"
	});*/

	app.post(/\/channel/, function(req, res) {
		console.log('worker /channel - ' + cluster.worker.id)
		console.log("PATH: " + req.url)
		crossHeader(res)
		/*var a = filter(req.ip, res);
		if (a) {
			var query = url.parse(req.url, true).query;*/
			routes.channel(res, req);
		/*} else {
			error(res, req);
		}*/
	});

	app.get('*', function(req, res) {
		console.log('worker *' + cluster.worker.id)
		console.log("PATH: " + req.url)
		var a = filter(req.ip, res);
		if (a) {
			var rurl = decodeURIComponent(req.url);
			var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？%+_]", "gim");
			rurl = rurl.replace(pattern, function(a) {
				return "\\" + a;
			});
			console.log(rurl)
			try {
				res.render(rurl, function(err, html) {
					if (err) {
						console.log("->", err);
						//console.log("addFilter: " + req.ip)
						//if (!/{{(pic|userico)+}}/.test(decodeURIComponent(req.url))) addFilterIp(req.ip);
						routes.err(res, '404');
					}
				});
			} catch (e) {
				console.log("->", e);
				routes.err(res, '404');
			}
		} else {
			routes.err(res, '404');
		}
	});

	//callback();
};